//
//  ViewController.m
//  cherryproduct
//
//  Created by umessage on 14-8-4.
//  Copyright (c) 2014年 cherry. All rights reserved.
//

#import "CPHomeViewController.h"
#import "HttpNewBaseService.h"
#import "ClassListViewController.h"
#import "GroupDetailsViewController.h"
#import "ProductReviewViewController.h"
#import "FIGoodsDetailsViewController.h"

@interface CPHomeViewController ()<UIAlertViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
{
    UICollectionView * collection_view;
}

@property(nonatomic, strong) UISegmentedControl * segmentedControl;

@property(nonatomic, strong) UIView * baseview_3;

@property(nonatomic, strong) MainTodayGroup * view_0;
@property(nonatomic, strong) MainClassGroup * view_1;
@property(nonatomic, strong) MainViewFutureGroup * view_2;

@end

@implementation CPHomeViewController


-(void)navtitle //顶端三个按钮
{
    if (YES) {
        CGFloat width = self.myView.frame.size.width;
        
        self.segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(10, 5, width-20, 35)];
        self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
        [self.segmentedControl insertSegmentWithTitle:@"今日" atIndex:0 animated:YES];
        [self.segmentedControl insertSegmentWithTitle:@"口碑" atIndex:1 animated:NO];
        [self.segmentedControl insertSegmentWithTitle:@"即将" atIndex:2 animated:NO];
        self.segmentedControl.segmentedControlStyle= UISegmentedControlStyleBar;
        self.segmentedControl.tintColor= RGBCOLOR(251, 201, 0);
        
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
                             [UIColor whiteColor], UITextAttributeTextColor,
                             [UIFont systemFontOfSize:17], UITextAttributeFont,
                             nil];
        NSDictionary *dic_nomal = [NSDictionary dictionaryWithObjectsAndKeys:
                             [UIColor blackColor], UITextAttributeTextColor,
                             [UIFont systemFontOfSize:17], UITextAttributeFont,
                             nil];
        
        [self.segmentedControl setTitleTextAttributes:dic forState:UIControlStateSelected];
        [self.segmentedControl setTitleTextAttributes:dic_nomal forState:UIControlStateNormal];
        self.segmentedControl.selectedSegmentIndex = 0;
        [self.myView addSubview:self.segmentedControl];
        
        [self.segmentedControl addTarget:self
                                  action:@selector(controlPressed:)
                        forControlEvents:UIControlEventValueChanged];
        
        //baseview
        CGRect r = CGRectMake(0, 47, self.myView.frame.size.width*3, self.myView.frame.size.height-47);
        
        self.baseview_3 = [[UIView alloc] initWithFrame:r];
        self.baseview_3.backgroundColor=[UIColor whiteColor];
        [self.myView addSubview:self.baseview_3];
        
        for (int i = 0; i < 3; i++) {
            CGFloat width = self.myView.frame.size.width;
            CGFloat height = self.baseview_3.frame.size.height;
            
            if (i == 0) {
                CGRect rect = CGRectMake(0, 0, width, height);
                self.view_0 = [[MainTodayGroup alloc] initWithFrame:rect];
                self.view_0.tag = 1001;
                self.view_0.delegate = self;
                [self.baseview_3 addSubview:self.view_0];
            }
            if (i == 1) {
                CGRect rect = CGRectMake(width, 0, width, height);
                self.view_1 = [[MainClassGroup alloc] initWithFrame:rect];
                self.view_1.tag = 1002;
                self.view_1.delegate = self;
                [self.baseview_3 addSubview:self.view_1];
            }
            if (i == 2) {
                CGRect rect = CGRectMake(width*2, 0, width, height);
                self.view_2 = [[MainViewFutureGroup alloc] initWithFrame:rect];
                self.view_2.tag = 1003;
                self.view_2.delegate = self;
                [self.baseview_3 addSubview:self.view_2];
            }
        }
        
        return;
    }
    
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.myView.frame.size.width, 47)];
    view.backgroundColor = [UIColor whiteColor];
    UIImage* imagetitle = [UIImage imageNamed:@"navmainviewtitle"];
    UIImageView* imageviewtitle = [[UIImageView alloc] initWithFrame:CGRectMake((view.frame.size.width-imagetitle.size.width)/2, (view.frame.size.height-imagetitle.size.height)/2, imagetitle.size.width, imagetitle.size.height)];
    imageviewtitle.image = imagetitle;
    [view addSubview:imageviewtitle];
    
    UIImage* leftimage = [UIImage imageNamed:@"todaytitle"];
    UIImage* middleimage = [UIImage imageNamed:@"classtitle"];
    UIImage* rightimage = [UIImage imageNamed:@"fufruetitle"];
    int allwith = leftimage.size.width+middleimage.size.width+rightimage.size.width;
    int x = (self.myView.frame.size.width-allwith)/2;
    for(int i = 0 ; i < 3 ; i ++)
    {
        int with = 0;
        int height = 0;
        if(i==0)
        {
            with = leftimage.size.width;
            height = leftimage.size.height;
        }
        if(i==1)
        {
            with = middleimage.size.width;
            height = middleimage.size.height;
        }
        if(i==2)
        {
            with = rightimage.size.width;
            height = rightimage.size.height;
        }
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(x, (view.frame.size.height-leftimage.size.height)/2,with ,height)];
        button.backgroundColor = [UIColor clearColor];
        [button addTarget:self action:@selector(titletouchdown:) forControlEvents:UIControlEventTouchDown];
        button.tag = 200+i;
        if(i==0)
        {
            [button setBackgroundImage:leftimage forState:UIControlStateNormal];
            [button setTitle:@"热点" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            
        }
        if(i==1)
        {
            [button setBackgroundImage:nil forState:UIControlStateNormal];
            [button setTitle:@"口碑" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
        }
        if(i==2)
        {
            [button setBackgroundImage:nil forState:UIControlStateNormal];
            [button setTitle:@"即将" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
        }
        [view addSubview:button];
        x= x+with;
    }
    view.tag = 9821;
    [self.myView addSubview:view]; //顶部三个按钮导航
    
}

- (void)controlPressed:(UISegmentedControl*)sender
{
//    [[self.myView viewWithTag:1001] removeFromSuperview];//移除今日团购
//    [[self.myView viewWithTag:1002] removeFromSuperview];//移除今日口碑
//    [[self.myView viewWithTag:1003] removeFromSuperview];//移除未来团购
    
    if (sender.selectedSegmentIndex==0) {
        [self goPage0];
        
        [MobClick beginLogPageView:@"HomePageToday"];
    }
    if (sender.selectedSegmentIndex==1) {
        [self goPage1];
        
        [MobClick beginLogPageView:@"HomePageKoubei"];
    }
    if (sender.selectedSegmentIndex==2) {
        [self goPage2];
        
        [MobClick beginLogPageView:@"HomePageJiJiang"];
    }
}

- (void)goPage0
{
    [UIView animateWithDuration:0.3 animations:^{
        CGFloat width = self.myView.frame.size.width;
        CGFloat height = self.myView.frame.size.height;
        
        CGRect r = CGRectMake(0, 47, width*3, height-47);
        self.baseview_3.frame=r;
    } completion:nil];
    
    [self.view startLoadingAnimation];
    [[HttpNewBaseService sharedClient] getRequestOperation:@""
                                           paramDictionary:nil
                                              onCompletion:^(NSString *responseString)
    {
        [self.view stopLoadingAnimation];

        //[self showAlertView:@"JPush信息" subtitle:[APService registrationID]];
        
        @try {
            NSDictionary * jsondic = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
            if ([jsondic objectForKey:@"data"] &&
               ([[jsondic objectForKey:@"data"] isKindOfClass:[NSDictionary class]] ||
                [[jsondic objectForKey:@"data"] isKindOfClass:[NSArray class]]))
            {
                datajson = [[NSMutableDictionary alloc] initWithDictionary:jsondic copyItems:YES];
                
//                CGRect r = CGRectMake(0, 47, self.myView.frame.size.width, self.myView.frame.size.height-47);
//                MainTodayGroup * page0 = [[MainTodayGroup alloc] initWithFrame:r];
//                page0.tag = 1001;
//                page0.delegate = self;
//                NSMutableDictionary * dicdata = [[NSMutableDictionary alloc] init];
//                [dicdata setObject:[jsondic objectForKey:@"data"] forKey:@"data"];
//                [page0 getusedata:dicdata];
//                [self.myView addSubview:page0]; //添加今日团购
                
                NSMutableDictionary * dicdata = [[NSMutableDictionary alloc] init];
                [dicdata setObject:[jsondic objectForKey:@"data"] forKey:@"data"];
                
                self.view_0.str_now_time = [[dicdata objectForKey:@"now_time"] longLongValue];
                [self.view_0 getusedata:dicdata];
                
            } else {
                [self showAlertView:@"温馨提示" subtitle:@"没有今日的团购了,等等吧!"];
            }
            
        } @catch (NSException *exception) {
            [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
        } @finally { }
    } onError:^(NSError *error) {
        [self.view stopLoadingAnimation];
        [self showAlertView:@"温馨提示" subtitle:@"网络断了"];
    } onAnimated:NO];
}

- (void)goPage1
{
    [UIView animateWithDuration:0.3 animations:^{
        CGFloat width = self.myView.frame.size.width;
        CGFloat height = self.myView.frame.size.height;
        
        CGRect r = CGRectMake(-width, 47, width*3, height-47);
        self.baseview_3.frame=r;
    } completion:nil];
    
    [self.view startLoadingAnimation];
    [[HttpNewBaseService sharedClient] getRequestOperation:@"index/category" paramDictionary:nil onCompletion:^(NSString *responseString) {
        [self.view stopLoadingAnimation];
        @try {
            NSDictionary *jsondic = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
            if ([jsondic objectForKey:@"data"] &&
                ([[jsondic objectForKey:@"data"] isKindOfClass:[NSArray class]]))
            {
                datajson = [[NSMutableDictionary alloc] initWithDictionary:jsondic copyItems:YES];
                
//                CGFloat width = self.myView.frame.size.width;
//                CGRect rect = CGRectMake(0, 47, width, self.myView.frame.size.height-47);
//                MainClassGroup * page1 = [[MainClassGroup alloc] initWithFrame:rect];
//                page1.tag = 1002;
//                page1.delegate = self;
//                [page1 getusedata:jsondic];
//                
//                [self.myView addSubview:page1]; //添加今日koubei
                
                [self.view_1 getusedata:jsondic];
                
            } else {
                [self showAlertView:@"温馨提示" subtitle:@"没有口碑了,等等吧!"];
            }
        } @catch (NSException *exception) {
            [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
        } @finally {}
    } onError:^(NSError *error) {
        [self.view stopLoadingAnimation];
        [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
    } onAnimated:YES]; //今日团购
}

- (void)goPage2
{
    [UIView animateWithDuration:0.3 animations:^{
        CGFloat width = self.myView.frame.size.width;
        CGFloat height = self.myView.frame.size.height;
        
        CGRect r = CGRectMake(-2*width, 47, width*3, height-47);
        self.baseview_3.frame=r;
    } completion:nil];
    
    [self.view startLoadingAnimation];
    [[HttpNewBaseService sharedClient] getRequestOperation:@"index/announce"
                                           paramDictionary:nil
                                              onCompletion:^(NSString *responseString)
    {
        [self.view stopLoadingAnimation];
        @try {
            NSDictionary *jsondic = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
            if([jsondic objectForKey:@"data"]&&([[jsondic objectForKey:@"data"] isKindOfClass:[NSArray class]]))
            {
                datajson = [[NSMutableDictionary alloc] initWithDictionary:jsondic copyItems:YES];
                
//                CGFloat width = self.myView.frame.size.width;
//                CGRect rect = CGRectMake(0, 47, width, self.myView.frame.size.height-47);
//                MainViewFutureGroup * page2 = [[MainViewFutureGroup alloc] initWithFrame:rect];
//                page2.tag = 1003;
//                page2.delegate = self;
//                [page2 getusedata:datajson];
//                [self.myView addSubview:page2]; //添加未来团购
                
                [self.view_2 getusedata:datajson];
                
            } else {
                [self showAlertView:@"温馨提示" subtitle:@"没有即将的团购了,等等吧!"];
            }
        } @catch (NSException *exception) {
            [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
        } @finally {}
    } onError:^(NSError *error) {
        [self.view stopLoadingAnimation];
        [self showAlertView:@"温馨提示" subtitle:@"网络断了"];
    } onAnimated:YES]; //今日团购
}


-(void)titletouchdown:(UIButton*)button //顶部三个按钮点击
{
    UIImage* leftimage = [UIImage imageNamed:@"todaytitle"];
    UIImage* middleimage = [UIImage imageNamed:@"classtitle"];
    UIImage* rightimage = [UIImage imageNamed:@"fufruetitle"];
    
    for(int i = 0 ; i < 3 ; i++)
    {
        UIButton* button = (UIButton*)[[self.myView viewWithTag:9821] viewWithTag:200+i];
        [button setBackgroundImage:nil forState:UIControlStateNormal];
        if(i==0)
        {
            [button setTitle:@"热点" forState:UIControlStateNormal];
            
        }
        if(i==1)
        {
            [button setTitle:@"口碑" forState:UIControlStateNormal];
            
        }
        if(i==2)
        {
            [button setTitle:@"即将" forState:UIControlStateNormal];
            
        }
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    [[self.view viewWithTag:1001] removeFromSuperview];//移除今日团购
    [[self.view viewWithTag:1002] removeFromSuperview];//移除今日口碑
    [[self.view viewWithTag:1003] removeFromSuperview];//移除未来团购

    if(button.tag==200)
    {
        [button setBackgroundImage:leftimage forState:UIControlStateNormal];
        [button setTitle:@"今日" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
       
        [self.view startLoadingAnimation];
        [[HttpNewBaseService sharedClient] getRequestOperation:@"" paramDictionary:nil onCompletion:^(NSString *responseString) {
            [self.view stopLoadingAnimation];
            @try {
                NSDictionary *jsondic = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
                if([jsondic objectForKey:@"data"]&&([[jsondic objectForKey:@"data"] isKindOfClass:[NSDictionary class]]||[[jsondic objectForKey:@"data"] isKindOfClass:[NSArray class]]))
                {
                    datajson = [[NSMutableDictionary alloc] initWithDictionary:jsondic copyItems:YES];
                    MainTodayGroup* todaygroup = [[MainTodayGroup alloc] initWithFrame:CGRectMake(0, 47, self.myView.frame.size.width, self.myView.frame.size.height-47)];
                    todaygroup.tag = 1001;
                    todaygroup.delegate = self;
                    NSMutableDictionary* dicdata = [[NSMutableDictionary alloc] init];
                    [dicdata setObject:[jsondic objectForKey:@"data"] forKey:@"data"];
                    [todaygroup getusedata:dicdata];
                    [self.myView addSubview:todaygroup]; //添加今日团购
                    
                }
                else
                {
                    
                    [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
                    
                }
                
                NSLog(@"%@",responseString);
            }
            @catch (NSException *exception) {
                [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
            }
            @finally {
                
            }
        } onError:^(NSError *error) {
            [self.view stopLoadingAnimation];
            [self showAlertView:@"温馨提示" subtitle:@"网络断了"];
        } onAnimated:NO];
    }
    if(button.tag==201)
    {
        [button setBackgroundImage:middleimage forState:UIControlStateNormal];
        [button setTitle:@"口碑" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        
        [self.view startLoadingAnimation];
        [[HttpNewBaseService sharedClient] getRequestOperation:@"index/category" paramDictionary:nil onCompletion:^(NSString *responseString) {
            [self.view stopLoadingAnimation];
            @try {
                NSDictionary *jsondic = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
                if([jsondic objectForKey:@"data"]&&([[jsondic objectForKey:@"data"] isKindOfClass:[NSArray class]]))
                {
                    datajson = [[NSMutableDictionary alloc] initWithDictionary:jsondic copyItems:YES];
                    MainClassGroup*class = [[MainClassGroup alloc] initWithFrame:CGRectMake(0, 47, self.myView.frame.size.width, self.myView.frame.size.height-47)];
                    class.tag = 1002;
                    class.delegate = self;
                    [class getusedata:jsondic];
                    [self.myView addSubview:class]; //添加今日koubei
                    
                }
                else
                {
                    
                    [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
                    
                }
                
                NSLog(@"%@",responseString);
            }
            @catch (NSException *exception) {
                [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
                
            }
            @finally {
                ;
            }
            
        } onError:^(NSError *error) {
            [self.view stopLoadingAnimation];
            [self showAlertView:@"温馨提示" subtitle:@"网络断了！"];
        } onAnimated:YES]; //今日团购
            
    }
    if(button.tag==202)
    {
        NSLog(@"今日预告");
        [button setBackgroundImage:rightimage forState:UIControlStateNormal];
        [button setTitle:@"预告" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [self.view startLoadingAnimation];
        [[HttpNewBaseService sharedClient] getRequestOperation:@"index/announce" paramDictionary:nil onCompletion:^(NSString *responseString) {
            [self.view stopLoadingAnimation];
            @try {
                NSDictionary *jsondic = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
                if([jsondic objectForKey:@"data"]&&([[jsondic objectForKey:@"data"] isKindOfClass:[NSArray class]]))
                {
                    datajson = [[NSMutableDictionary alloc] initWithDictionary:jsondic copyItems:YES];
                    MainViewFutureGroup* mainfuture = [[MainViewFutureGroup alloc] initWithFrame:CGRectMake(0, 47, self.myView.frame.size.width, self.myView.frame.size.height-47)];
                    mainfuture.tag = 1003;
                    mainfuture.delegate = self;
                    [mainfuture getusedata:datajson];
                    [self.myView addSubview:mainfuture]; //添加未来团购
                    
                }
                else
                {
                    
                    [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
                    
                }
                
                NSLog(@"%@",responseString);
            }
            @catch (NSException *exception) {
                [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
                
            }
            @finally {
                ;
            }
        } onError:^(NSError *error) {
            [self.view stopLoadingAnimation];
            [self showAlertView:@"温馨提示" subtitle:@"网络断了"];
        } onAnimated:YES]; //今日团购
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    isfuturnpage = NO;
   	// Do any additional setup after loading the view, typically from a nib.
    [self addNavBarButton:RIGHT_SEARCH_CENTRE];
    [self hidesBackButton:YES];
    [self navtitle]; //建立顶三个按钮
    [self controlPressed:self.segmentedControl];
    //UIButton* button = (UIButton*)[[self.myView viewWithTag:9821] viewWithTag:200];
    //[self titletouchdown:button];//模拟点击今日团购
    [self getVersion];
    
    //添加引导页面
    [self goFirstPage];
}

- (void)goFirstPage
{
    //读取沙盒数据
    NSUserDefaults * settings1 = [NSUserDefaults standardUserDefaults];
    NSString *key1 = [NSString stringWithFormat:@"is_first"];
    NSString *value = [settings1 objectForKey:key1];
    
    if (!value) {
        UICollectionViewFlowLayout * layout= [[UICollectionViewFlowLayout alloc] init];
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        [layout setItemSize:self.view.frame.size]; //设置每个cell显示数据的宽和高必须
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumLineSpacing=0;
        layout.minimumInteritemSpacing=0;
        //layout.footerReferenceSize=CGSizeMake(self.frame.size.width, 50);
        
        collection_view = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
        collection_view.backgroundColor=[UIColor whiteColor];
        collection_view.delegate=self;
        collection_view.dataSource=self;
        collection_view.pagingEnabled=YES;
        [collection_view registerClass:[SGSeeBigImgCell class] forCellWithReuseIdentifier:@"SGSeeBigImgCell"];
        [self.view addSubview:collection_view];
        
        //写入数据
        NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];
        NSString * key = [NSString stringWithFormat:@"is_first"];
        [setting setObject:[NSString stringWithFormat:@"false"] forKey:key];
        [setting synchronize];
    }
}
#pragma mark -
#pragma mark UICollectionViewDataSource UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
{
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    SGSeeBigImgCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SGSeeBigImgCell" forIndexPath:indexPath];
    
    cell.btn_click.tag=indexPath.row;
    [cell.btn_click addTarget:self action:@selector(doFirstPageDD:) forControlEvents:UIControlEventTouchUpInside];
    
    NSInteger index = indexPath.row+1;
    NSString * str_img_name = [NSString stringWithFormat:@"go5_%d", index];
    cell.showImg.image=[UIImage imageNamed:str_img_name];
    
    return cell;
}

- (void)doFirstPageDD:(UIButton*)sender
{
    if (sender.tag==3) {
        collection_view.hidden=YES;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)centreButtonAction:(UIButton *)sender
{
    //NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //NSString * username = [defaults objectForKey:@"USERNAME"];
    //NSString * password = [defaults objectForKey:@"USERPASSWORD"];
    
    NSString * loginInfo = [[LoginViewController shareUserInfo] objectForKey:KEY_HAVE_LOGIN];
    
    if (loginInfo && [loginInfo isKindOfClass:[NSString class]] && [loginInfo isEqualToString:@"1"])
    {
        [self pushToViewController:@"MemberCentreViewController" animated:YES];
    }
    else
    {
        [self pushToViewController:@"LoginViewController" animated:YES];
    }
}

- (void)searchButtonAction:(UIButton *)sender
{
    [self pushToViewController:@"SearchViewController" animated:YES];
}

#pragma mark -
#pragma mark Today
-(void)MainTodayGroupDownIndex:(int)indexdown //点击今日团购的序列
{
    isfuturnpage = NO;
    
    /*
    GroupDetailsViewController * detail = [[GroupDetailsViewController alloc] init];
    detail.stringtitle = [[[datajson objectForKey:@"data"] objectAtIndex:indexdown] objectForKey:@"title"];
    detail.timeover  =[[[[datajson objectForKey:@"data"] objectAtIndex:indexdown] objectForKey:@"end_time"] doubleValue];
    detail.stringid = [[[datajson objectForKey:@"data"] objectAtIndex:indexdown] objectForKey:@"id"];
    detail.istodaygroup = YES;
    detail.stringtitlecolor = [[[datajson objectForKey:@"data"] objectAtIndex:indexdown] objectForKey:@"color"];
    [self.navigationController pushViewController:detail animated:YES];
    */
//    ProductReviewViewController* a = [[ProductReviewViewController alloc] init];
//    [self.navigationController pushViewController:a animated:YES];
    
    FIGoodsDetailsViewController * vc = [[FIGoodsDetailsViewController alloc] init];
    vc.str_goods_id = [[[datajson objectForKey:@"data"] objectAtIndex:indexdown] objectForKey:@"id"];
    vc.str_color_bg = [[[datajson objectForKey:@"data"] objectAtIndex:indexdown] objectForKey:@"color"];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)MainClassGroupDownIndex:(int)indexdown numrow:(int)row //点击口碑的顺序
{
    isfuturnpage = NO;
    if(row==-1) //点击一级分类
    {
        NSArray * array_d = [datajson objectForKey:@"data"];
        if (array_d && [array_d isKindOfClass:[NSArray class]]) {
            if (indexdown < [array_d count]) {
                NSDictionary * dic = [array_d objectAtIndex:indexdown];
                NSString* stringfirst =[dic objectForKey:@"id"];
                NSString* stringtitle = [dic objectForKey:@"title"];
                NSMutableDictionary* dicdataparam = [[NSMutableDictionary alloc] init];
                [dicdataparam setObject:stringfirst forKey:@"cid"];
                [dicdataparam setObject:stringtitle forKey:@"title"];
                
                ClassListViewController* list = [[ClassListViewController alloc] init];
                list.issearch = NO;
                list.str_name_0=stringfirst;
                list.str_name_1=@"全部";
                [list getuseparamclasslistdata:dicdataparam];
                [self.navigationController pushViewController:list animated:YES];
            }
        }
    }
    else
    {
        NSArray * array_d = [datajson objectForKey:@"data"];
        
        if (array_d && [array_d isKindOfClass:[NSArray class]]) {
            if (indexdown < [array_d count]) {
                
                NSDictionary * dic = [array_d objectAtIndex:indexdown];
                NSString * stringfirst =[dic objectForKey:@"id"];
                
                if (dic && [dic isKindOfClass:[NSDictionary class]]) {
                    NSArray * arr = [dic objectForKey:@"s_class"];
                    if (arr && [arr isKindOfClass:[NSArray class]]) {
                        if (row < [arr count]) {
                            NSDictionary * item_data = [arr objectAtIndex:row];
                            if (item_data && [item_data isKindOfClass:[NSDictionary class]]) {
                                NSString* stringsec = [item_data objectForKey:@"id"];
                                NSString* stringtitle = [item_data objectForKey:@"title"];
                                
                                NSMutableDictionary* dicdataparam = [[NSMutableDictionary alloc] init];
                                [dicdataparam setObject:stringtitle forKey:@"title"];
                                [dicdataparam setObject:stringsec forKey:@"cid"];
                                
                                ClassListViewController* list = [[ClassListViewController alloc] init];
                                list.issearch = NO;
                                list.str_name_0=stringfirst;
                                list.str_name_1=stringsec;
                                [list getuseparamclasslistdata:dicdataparam];
                                [self.navigationController pushViewController:list animated:YES];
                            }
                        }
                    }
                }//end
            }
        }
        
    }
}

- (void)pushGoPage:(NSString*)groupid
{
    FIGoodsDetailsViewController * vc = [[FIGoodsDetailsViewController alloc] init];
    vc.str_goods_id = groupid;
    vc.str_color_bg = @"#FBC900";//251 201 0
    vc.m_b_is_notice=YES;
    vc.m_b_is_have_toto=YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)MainViewFutureGroupDownIndex:(int)indexdown numrow:(int)row //点击未来团购的某一项目 indexdown0为进入下一集页面1为上线提醒
{
    isfuturnpage = YES;

    if(indexdown==0) //进入团购详情
    {
        /*
        GroupDetailsViewController * detail = [[GroupDetailsViewController alloc] init];
        detail.stringtitle = [[[datajson objectForKey:@"data"] objectAtIndex:row] objectForKey:@"title"];
        detail.timeover  =[[[[datajson objectForKey:@"data"] objectAtIndex:row] objectForKey:@"start_time"] doubleValue];
        detail.stringid = [[[datajson objectForKey:@"data"] objectAtIndex:row] objectForKey:@"id"];
        detail.istodaygroup = NO;
        int temp =[[[[datajson objectForKey:@"data"] objectAtIndex:row] objectForKey:@"is_apply"] intValue];
        detail.isringup = temp;
        [self.navigationController pushViewController:detail animated:YES];
        */
        FIGoodsDetailsViewController * vc = [[FIGoodsDetailsViewController alloc] init];
        vc.str_goods_id = [[[datajson objectForKey:@"data"] objectAtIndex:row] objectForKey:@"id"];
        vc.str_color_bg = @"#FBC900";//251 201 0
        vc.m_b_is_notice=YES;
        vc.m_b_is_have_toto=[[[[datajson objectForKey:@"data"] objectAtIndex:row] objectForKey:@"is_apply"] integerValue];
        [self.navigationController pushViewController:vc animated:YES];
        
    } else {
        //判断是否登录
        NSString * loginInfo = [[LoginViewController shareUserInfo] objectForKey:KEY_HAVE_LOGIN];
        
        if (loginInfo && [loginInfo isKindOfClass:[NSString class]] && [loginInfo isEqualToString:@"1"]){
            
        } else {
            LoginViewController * vc = [[LoginViewController alloc] init];
            vc.m_is_come_other=YES;
            [self.navigationController pushViewController:vc animated:YES];
            return;
        }
    
        NSArray * array_data = [datajson objectForKey:@"data"];
        if (array_data && [array_data isKindOfClass:[NSArray class]]) {
            if (row < [array_data count]) {
                NSDictionary * item_dic = [array_data objectAtIndex:row];
                if (item_dic && [item_dic isKindOfClass:[NSDictionary class]]) {
                    
                    NSString * is_apply = [item_dic objectForKey:@"is_apply"];
                    NSString * str_id = [item_dic objectForKey:@"id"];
                    
                    if (is_apply && [is_apply isKindOfClass:[NSString class]] && ![is_apply isEqual:@""]) {
                        if ([is_apply isEqualToString:@"0"]) {//添加提醒
                            if (str_id && [str_id isKindOfClass:[NSString class]] && ![str_id isEqual:@""]) {
                                NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
                                [dic setObject:str_id forKey:@"id"];
                                //添加团购提醒
                                [[HttpNewBaseService sharedClient] getRequestOperation:@"groupon/alarm_join" paramDictionary:dic onCompletion:^(NSString *responseString) {
                                    NSDictionary * jsondic = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
                                    if([[jsondic objectForKey:@"status"] intValue]==1)//成功
                                    {
                                        //[self showAlertView:@"温馨提示" subtitle:[jsondic objectForKey:@"message"]];
                                        [self viewWillAppear:YES];
                                        [self goPage2];
                                    } else {
                                        [self showAlertView:@"温馨提示" subtitle:[jsondic objectForKey:@"message"]];
                                    }
                                } onError:^(NSError *error) {
                                    [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
                                } onAnimated:YES];
                            }//...
                        } else {//去掉提醒
                            NSMutableDictionary* dic = [[NSMutableDictionary alloc] init];
                            [dic setObject:str_id forKey:@"id"];
                            [[HttpNewBaseService sharedClient] getRequestOperation:@"groupon/alarm_cancel" paramDictionary:dic onCompletion:^(NSString *responseString) {
                                NSDictionary *jsondic = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
                                if ([[jsondic objectForKey:@"status"] intValue]==1)//成功
                                {
                                    //[self showAlertView:@"温馨提示" subtitle:[jsondic objectForKey:@"message"]];
                                    [self viewWillAppear:YES];
                                    [self goPage2];
                                } else {
                                    [self showAlertView:@"温馨提示" subtitle:[jsondic objectForKey:@"message"]];
                                }
                                
                            } onError:^(NSError *error) {
                                [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
                            } onAnimated:YES];
                        }
                    }
                }
            }
        }
        
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if(isfuturnpage)
    {

    }
    if (self.segmentedControl.selectedSegmentIndex==2) {
        [self goPage2];
    }
}

#pragma mark -
#pragma mark Version update
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    if (buttonIndex==0) {
        if (self.str_update_url != nil &&
            [self.str_update_url isKindOfClass:[NSString class]] &&
            ![self.str_update_url isEqualToString:@""]) {
            
            UIApplication * application = [UIApplication sharedApplication];
            [application openURL:[NSURL URLWithString:self.str_update_url]];
        }
    }
}
- (void)getVersion
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] initWithCapacity:2];
    [dic setObject:@"1" forKey:@"device_type"];
    [dic setObject:app_Version forKey:@"version"];
    
    [[HttpNewBaseService sharedClient] getRequestOperation:@"member/version_check"
                                           paramDictionary:nil
                                              onCompletion:^(NSString *responseString)
     {
         //...
         NSDictionary * dic_res = [responseString JSONValue];
         if (dic_res && [dic_res isKindOfClass:[NSDictionary class]]) {
             
             NSInteger status = [[dic_res objectForKey:@"status"] integerValue];
             if (status == 1) {//有更新
                 NSDictionary * dic_data = [dic_res objectForKey:@"data"];
                 NSInteger force = [[dic_data objectForKey:@"force"] integerValue];
                 NSString * str_message = @"";
                 if ([dic_data objectForKey:@"change"] &&
                     [[dic_data objectForKey:@"change"] isKindOfClass:[NSString class]]) {
                     str_message = [dic_data objectForKey:@"change"];
                 };
                 
                 if ([dic_data objectForKey:@"url"] &&
                     [[dic_data objectForKey:@"url"] isKindOfClass:[NSString class]]) {
                     self.str_update_url = [dic_data objectForKey:@"url"];
                 };
                 
                 if (force == 1) {
                     //强制更新
                     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"软件更新"
                                                                      message:str_message
                                                                     delegate:self
                                                            cancelButtonTitle:@"确定"
                                                            otherButtonTitles:nil, nil];
                     [alert show];
                 } else {
                     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"软件更新"
                                                                      message:str_message
                                                                     delegate:self
                                                            cancelButtonTitle:@"确定"
                                                            otherButtonTitles:@"取消", nil];
                     [alert show];
                 }
             } else {
                 //[self showAlertView:@"温馨提示" subtitle:[dic_res objectForKey:@"message"]];
             }
             
         } else {
             //[self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
         }
         
     } onError:^(NSError *error) {
         //[self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
     } onAnimated:YES];
}

@end







#pragma mark -
#pragma mark co_cell
@implementation SGSeeBigImgCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 在此添加
        // 初始化时加载collectionCell.xib文件
        if (self.showImg == nil) {
            self.showImg = [[UIImageView alloc] initWithFrame:self.bounds];
            [self addSubview:self.showImg];
        }
        
        if (self.btn_click == nil) {
            CGFloat width = self.frame.size.width;
            CGFloat height = self.frame.size.height;
            
            self.btn_click = [[UIButton alloc] initWithFrame:CGRectMake((width-150)/2, height-100, 150, 50)];
            self.btn_click.backgroundColor=[UIColor clearColor];
            [self addSubview:self.btn_click];
        }

    }
    return self;
}

@end



